package org.mozhu.mboot.core.config.jasypt;

import com.ulisesbocchio.jasyptspringboot.InterceptionMode;
import com.ulisesbocchio.jasyptspringboot.configuration.EncryptablePropertyResolverConfiguration;
import org.jasypt.encryption.StringEncryptor;
import org.mozhu.mboot.core.config.ApplicationConfigurationAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;

@Configuration
@ConditionalOnClass(StringEncryptor.class)
@Import(EncryptablePropertyResolverConfiguration.class)
public class JasyptEncryptAutoConfiguration {

    @Bean
    public static PriorityOrderedEnableEncryptablePropertiesBeanFactoryPostProcessor enableEncryptablePropertySourcesPostProcessor(ConfigurableEnvironment environment) {
        boolean proxyPropertySources = environment.getProperty("jasypt.encryptor.proxyPropertySources", Boolean.TYPE, false);
        InterceptionMode interceptionMode = proxyPropertySources ? InterceptionMode.PROXY : InterceptionMode.WRAPPER;
        return new PriorityOrderedEnableEncryptablePropertiesBeanFactoryPostProcessor(environment, interceptionMode) {
            @Override
            public int getOrder() {
                return ApplicationConfigurationAutoConfiguration.LOWEST_PRECEDENCE_H;
            }
        };
    }
}
